using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class PassiveSystem : IClass
    {
        private BattleUnit unit;
        private List<SkillPassive> passiveSkills;
        
        void IClass.OnReset()
        {
            
        }

        public void Init(BattleUnit unit)
        {
            this.unit = unit;
            passiveSkills = new List<SkillPassive>();
            var skills = unit.Data.config.passiveSkills;
            if (skills != null)
            {
                for (int i = 0; i < skills.Count; i++)
                {
                    var skill = ClassManager.Get<SkillPassive>();
                    skill.Init(unit,skills[i]);
                    passiveSkills.Add(skill);
                }
            }
        }
        
        
        public void OnTransmit(ETransmitType type,BaseTransmitArg arg)
        {
            if (type == ETransmitType.RoundStart)
            {
                Trigger(1,unit);
            }
            else if (type == ETransmitType.GetHurt)
            {
                var triggerArg = arg as PassiveTriggerArg;
                Trigger(5,triggerArg.source);
            }
        }

        public void Update()
        {
            for (int i = 0; i < passiveSkills.Count; i++)
            {
                passiveSkills[i].Update();
            }
        }

        private void Trigger(int triggerType,BattleUnit target = null)
        {
            for (int i = 0; i < passiveSkills.Count; i++)
            {
                var skill = passiveSkills[i];
                if (skill.config.triggerType == triggerType)
                {
                    skill.Start(target);
                }
            }
        }
    }
    
   
}